package com.voice.call.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.voice.call.common.Const;
import com.voice.call.entity.AdvertOperateLog;
import com.voice.call.mapper.AdvertOperateLogMapper;
import com.voice.call.service.AdvertOperateLogService;

@Service
public class AdvertOperateLogServiceImpl implements AdvertOperateLogService{
	

  @Resource
  private AdvertOperateLogMapper luckyCreditPlayMapper;

   @Override
   public AdvertOperateLog addLuckyCreditPlay(String category,Integer user_id,Integer integral){
	   
	   //this.deleteLuckyCreditPlay(user_id,category);
	
	   AdvertOperateLog entity =new  AdvertOperateLog();
	   entity.setUserId(user_id);
	   entity.setCategory(category);
	   entity.setStatus(Const.Y);
	   entity.setIntegral(integral);
	   entity.setCtime(new Date());
	   entity.setMtime(new Date());
	   if (luckyCreditPlayMapper.insert(entity)>0) {
		  return entity;
	   }
	   return null;
   }
   
   @Override
   public AdvertOperateLog addMapLuckyCreditPlay(String category,Integer user_id,Map<String, Integer> integralMap) {
	   
	   if (integralMap == null) {
		  return null;
	   }
	   
	   AdvertOperateLog entity =new  AdvertOperateLog();
	   entity.setUserId(user_id);
	   entity.setCategory(category);
	   entity.setStatus(Const.Y);
	   if (integralMap.get("total")!=null) {
		   entity.setIntegral(integralMap.get("total"));
	   }
	   if (integralMap.get("star_integral")!=null) {
		   entity.setStarIntegral(integralMap.get("star_integral"));
	   }
	   if (integralMap.get("rand_num")!=null) {
		   entity.setRandNum(integralMap.get("rand_num"));
	   }
	   if (integralMap.get("lucky_integral")!=null) {
		   entity.setLuckyIntegral(integralMap.get("lucky_integral"));
	   }
	   entity.setCtime(new Date());
	   entity.setMtime(new Date());
	   if (luckyCreditPlayMapper.insert(entity)>0) {
		  return entity;
	   }
	   return null;
   }
   
   
   @Override
   public void deleteLuckyCreditPlay(Integer user_id,String category){
	   Map<String, Object> params = new HashMap<String, Object>();
	   params.put("userId", user_id);
	   params.put("category", category);
	   luckyCreditPlayMapper.deleteForCategory(params);
   }
   
   @Override
   public AdvertOperateLog getLuckyCreditPlayForId(Integer id){
	   
	   return luckyCreditPlayMapper.get(id);
   }
   @Override
   public boolean updateStatus(Integer id,String status,Integer ad_time){
	   
	   AdvertOperateLog entity =new  AdvertOperateLog();
	   entity.setStatus(status);
	   if (ad_time!=null) {
		   entity.setAdTime(ad_time);
	   }
	   entity.setMtime(new Date());
	   entity.setId(id);
	   return luckyCreditPlayMapper.update(entity) > 0?true:false;
   }
}
